package simple.flow.modules.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import simple.flow.exception.AppBootException;
import simple.flow.modules.system.dto.login.LoginDTO;
import simple.flow.modules.system.dto.login.RegisterDTO;
import simple.flow.modules.system.entity.SysUser;
import simple.flow.modules.system.mapper.SysUserMapper;
import simple.flow.modules.system.service.LoginService;
import simple.flow.modules.system.vo.login.LoginUserInfoVO;
import simple.flow.modules.system.vo.login.LoginVO;
import simple.flow.util.JwtUtil;
import simple.flow.util.PasswordUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

/**
 * 登录相关实现
 *
 * @author lhd
 * @since 2025/4/8 11:18
 */

@Service
@RequiredArgsConstructor
public class LoginServiceImpl implements LoginService {

    private final SysUserMapper sysUserMapper;

    @Override
    public LoginVO login(LoginDTO loginDTO) {
        // 获取公共的数据
        String captchaCode = loginDTO.getCaptchaCode();
        String captchaKey = loginDTO.getCaptchaKey();
        String loginMode = loginDTO.getLoginMode();

        // 用户账号
        String username = "";
        SysUser sysUser = null;

        // 进行验证码校验,并获得用户信息
        switch (loginMode) {
            // 密码登录
            case "pwd":
                // TODO 图片验证码校验

                // 校验密码是否正确
                username = loginDTO.getUsername();
                if (StringUtils.isEmpty(username)) {
                    throw new AppBootException("用户名不能为空");
                }
                sysUser = sysUserMapper.selectByUsername(username);
                if (sysUser == null) {
                    throw new AppBootException("用户未注册");
                }

                // 密码校验
                String password = loginDTO.getPassword();
                if (StringUtils.isEmpty(password)) {
                    throw new AppBootException("密码不能为空");
                }
                boolean match = PasswordUtil.match(password, sysUser.getPassword(), sysUser.getSalt());
                if (!match) {
                    throw new AppBootException("密码错误");
                }

                break;
            // 短信登录
            case "sms":
                // TODO 短信验证码校验

                // 获取用户信息
                String mobile = loginDTO.getMobile();
                if (StringUtils.isEmpty(mobile)) {
                    throw new AppBootException("手机号不能为空");
                }
                sysUser = sysUserMapper.selectByMobile(mobile);
                if (sysUser == null) {
                    throw new AppBootException("用户未注册");
                }

                break;
            // 邮箱登录
            case "email":
                // TODO 邮箱验证码校验

                // 获取用户信息
                String email = loginDTO.getEmail();
                if (StringUtils.isEmpty(email)) {
                    throw new AppBootException("邮箱不能为空");
                }
                sysUser = sysUserMapper.selectByEmail(email);
                if (sysUser == null) {
                    throw new AppBootException("用户未注册");
                }

                break;
            default:
                throw new RuntimeException("登录模式不支持");
        }

        // 封装返回数据
        LoginVO loginVO = new LoginVO();
        LoginUserInfoVO userInfoVO = BeanUtil.toBean(sysUser, LoginUserInfoVO.class);
        loginVO.setUserInfo(userInfoVO);

        // 生成token
        String token = JwtUtil.createToken(username);
        loginVO.setToken(token);

        // 返回数据
        return loginVO;
    }

    @Override
    public void logout() {
        SecurityUtils.getSubject().logout();
    }

    @Override
    public void register(RegisterDTO registerDTO) {
        String registerMode = registerDTO.getRegisterMode();

        // 用户注册的相关信息
        SysUser sysUser = null;
        String encryptPassword = null;
        String salt = null;
        String username = null;
        String mobile = null;
        String email = null;

        // 相关校验和信息初始化
        switch (registerMode) {
            case "pwd":
                // TODO 图片验证码校验

                // 查询相关信息
                username = registerDTO.getUsername();
                if (StringUtils.isEmpty(username)) {
                    throw new AppBootException("用户名不能为空");
                }
                sysUser = sysUserMapper.selectByUsername(username);
                if (sysUser != null) {
                    throw new AppBootException("该用户名已注册");
                }

                // 校验密码
                String password = registerDTO.getPassword();
                String confirmPassword = registerDTO.getConfirmPassword();
                if (StringUtils.isEmpty(password) || StringUtils.isEmpty(confirmPassword)) {
                    throw new AppBootException("密码不能为空");
                }
                if (!password.equals(confirmPassword)) {
                    throw new AppBootException("密码不一致");
                }

                // 初始化相关信息
                salt = PasswordUtil.generateSalt();
                encryptPassword = PasswordUtil.encrypt(password, salt);
                break;
            case "sms":
                // TODO 短信验证码校验

                // 校验是否注册过
                mobile = registerDTO.getPhone();
                if (StringUtils.isEmpty(mobile)) {
                    throw new AppBootException("手机号不能为空");
                }
                sysUser = sysUserMapper.selectByMobile(mobile);
                if (sysUser != null) {
                    throw new AppBootException("该手机号已注册");
                }

                // 生成随机密码
                salt = PasswordUtil.generateSalt();
                String randomPasswordMobile = mobile + "@" + RandomUtil.randomString(6);
                encryptPassword = PasswordUtil.encrypt(randomPasswordMobile, salt);

                // 生成用户名
                username = mobile;

                break;
            case "email":
                // TODO 邮箱验证码校验


                // 校验是否注册过
                email = registerDTO.getEmail();
                if (StringUtils.isEmpty(email)) {
                    throw new AppBootException("邮箱不能为空");
                }
                sysUser = sysUserMapper.selectByEmail(email);
                if (sysUser == null) {
                    throw new AppBootException("该邮箱已注册");
                }

                // 生成随机密码
                salt = PasswordUtil.generateSalt();
                String randomPasswordEmail = email + "@" + RandomUtil.randomString(6);
                encryptPassword = PasswordUtil.encrypt(randomPasswordEmail, salt);

                // 生成用户名
                username = email;

                break;
            default:
                throw new RuntimeException("注册模式不支持");
        }

        // 注册用户
        sysUser = new SysUser();
        sysUser.setUsername(username);
        sysUser.setMobile(mobile);
        sysUser.setEmail(email);
        sysUser.setPassword(encryptPassword);
        sysUser.setSalt(salt);
        sysUserMapper.insert(sysUser);
    }
}
